package com.lanyou.esb.entity;

import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EntityListeners;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Transient;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.apache.commons.lang3.time.FastDateFormat;
import org.hibernate.annotations.GenericGenerator;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.lanyou.cook.common.Constants;
import com.lanyou.cook.common.DefaultDateJsonSerializer;

/**
 * 
 * @author Davey.wu
 */
@Entity
@EntityListeners({ AuditingEntityListener.class })
@Table(name = "T_ESB_BU_IF_TOKEN")
public class Token {
	/** 接口令牌表ID **/
	private String id;

	/** 接口 **/
	private Http http;

	/** 接口类型 **/
	private String interfaceType;

	/** 接口名称 **/
	private String interfaceName;

	/** 接口编码 **/
	private String interfaceCode;

	/** 接口版本 **/
	private String interfaceVersion;

	/** 密钥 **/
	private String appKey;

	/** 密钥 有效期 **/
	private Date keyInvalidDate;

	/** 接口发布方系统ID **/
	private String interfacePubSystemId;

	/** 接口发布方系统编码 **/
	private String interfacePubSystemCode;

	/** 接口调用方系统ID **/
	private String interfaceCallSystemId;

	/** 接口调用方系统编码 **/
	private String interfaceCallSytemCode;

	/** 联系电话 **/
	private String linkTel;

	/** 令牌申请时间 **/
	private Date applyDate;

	/** 令牌有效时长 **/
	private int invalidTimes;

	/** 申请账号 **/
	private String applyUserCode;

	/** 访问令牌 **/
	private String accessToken;

	/** IP白名单 **/
	private String ipWhiteList;

	/** 是否可用 **/
	private String isEnable;

	/** 创建人 **/
	private String creator;

	/** 创建时间 **/
	private Date createdDate;

	/** 最后更新人员 **/
	private String modifier;

	/** 最后更新时间 **/
	private Date lastUpdatedDate;

	/** 当日已申请次数 **/
	private int curApplyQty = 0;

	/** 状态 **/
	private String status;

	private String fieldName;

	private String interfaceCallSytemName;

	private String interfacePubSystemName;

	private static FastDateFormat FASTDATEFORMAT_YYYYMMDDHHMMSS = FastDateFormat
			.getInstance(Constants.DATETIME_PATTERN_1);

	public static Specification<Token> idSpecification(String id) {
		return new Specification<Token>() {
			@Override
			public Predicate toPredicate(Root<Token> root,
					CriteriaQuery<?> query, CriteriaBuilder cb) {
				query.where(cb.equal(root.get("id"), id));
				return null;
			}
		};
	}

	@Id
	@GeneratedValue(generator = "uuidGenerator")
	@GenericGenerator(name = "uuidGenerator", strategy = "uuid")
	@Column(name = "IF_TOKEN_ID", length = 36)
	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	@Column(name = "IF_TYPE", length = 2)
	public String getInterfaceType() {
		return interfaceType;
	}

	public void setInterfaceType(String interfaceType) {
		this.interfaceType = interfaceType;
	}

	@Column(name = "IF_NAME", length = 50)
	public String getInterfaceName() {
		return interfaceName;
	}

	public void setInterfaceName(String interfaceName) {
		this.interfaceName = interfaceName;
	}

	@Column(name = "IF_CODE", length = 50)
	public String getInterfaceCode() {
		return interfaceCode;
	}

	public void setInterfaceCode(String interfaceCode) {
		this.interfaceCode = interfaceCode;
	}

	@Column(name = "IF_VERSION", length = 50)
	public String getInterfaceVersion() {
		return interfaceVersion;
	}

	public void setInterfaceVersion(String interfaceVersion) {
		this.interfaceVersion = interfaceVersion;
	}

	@Column(name = "APP_KEY", length = 50)
	public String getAppKey() {
		return appKey;
	}

	public void setAppKey(String appKey) {
		this.appKey = appKey;
	}

	@Column(name = "KEY_INVALID_DATE")
	public Date getKeyInvalidDate() {
		return keyInvalidDate;
	}

	public void setKeyInvalidDate(Date keyInvalidDate) {
		this.keyInvalidDate = keyInvalidDate;
	}

	@Column(name = "IF_PUB_SYSTEM_ID", length = 36)
	public String getInterfacePubSystemId() {
		return interfacePubSystemId;
	}

	public void setInterfacePubSystemId(String interfacePubSystemId) {
		this.interfacePubSystemId = interfacePubSystemId;
	}

	@Column(name = "IF_PUB_SYSTEM_CODE", length = 50)
	public String getInterfacePubSystemCode() {
		return interfacePubSystemCode;
	}

	public void setInterfacePubSystemCode(String interfacePubSystemCode) {
		this.interfacePubSystemCode = interfacePubSystemCode;
	}

	@Column(name = "IF_CALL_SYSTEM_ID", length = 36)
	public String getInterfaceCallSystemId() {
		return interfaceCallSystemId;
	}

	public void setInterfaceCallSystemId(String interfaceCallSystemId) {
		this.interfaceCallSystemId = interfaceCallSystemId;
	}

	@Column(name = "IF_CALL_SYSTEM_CODE", length = 50)
	public String getInterfaceCallSytemCode() {
		return interfaceCallSytemCode;
	}

	public void setInterfaceCallSytemCode(String interfaceCallSytemCode) {
		this.interfaceCallSytemCode = interfaceCallSytemCode;
	}

	@Column(name = "LINK_TEL", length = 50)
	public String getLinkTel() {
		return linkTel;
	}

	public void setLinkTel(String linkTel) {
		this.linkTel = linkTel;
	}

	@Column(name = "AK_APPLY_DATE")
	public Date getApplyDate() {
		return applyDate;
	}

	public void setApplyDate(Date applyDate) {
		this.applyDate = applyDate;
	}

	@Column(name = "INVALID_TIMES")
	public int getInvalidTimes() {
		return invalidTimes;
	}

	public void setInvalidTimes(int invalidTimes) {
		this.invalidTimes = invalidTimes;
	}

	@Column(name = "APPLY_USER_CODE", length = 20)
	public String getApplyUserCode() {
		return applyUserCode;
	}

	public void setApplyUserCode(String applyUserCode) {
		this.applyUserCode = applyUserCode;
	}

	@Column(name = "ACCESS_TOKEN", length = 50)
	public String getAccessToken() {
		return accessToken;
	}

	public void setAccessToken(String accessToken) {
		this.accessToken = accessToken;
	}

	@Column(name = "IP_WHITELIST", length = 500)
	public String getIpWhiteList() {
		return ipWhiteList;
	}

	public void setIpWhiteList(String ipWhiteList) {
		this.ipWhiteList = ipWhiteList;
	}

	@Column(name = "IS_ENABLE", length = 2)
	public String getIsEnable() {
		return isEnable;
	}

	public void setIsEnable(String isEnable) {
		this.isEnable = isEnable;
	}

	@Column(name = "CREATOR", length = 50)
	public String getCreator() {
		return creator;
	}

	public void setCreator(String creator) {
		this.creator = creator;
	}

	@Column(name = "CREATED_DATE")
	public Date getCreatedDate() {
		return createdDate;
	}

	public void setCreatedDate(Date createdDate) {
		this.createdDate = createdDate;
	}

	@Column(name = "MODIFIER", length = 50)
	public String getModifier() {
		return modifier;
	}

	public void setModifier(String modifier) {
		this.modifier = modifier;
	}

	@Column(name = "LAST_UPDATED_DATE")
	@JsonSerialize(using = DefaultDateJsonSerializer.class)
	@LastModifiedDate
	public Date getLastUpdatedDate() {
		return lastUpdatedDate;
	}

	public void setLastUpdatedDate(Date lastUpdatedDate) {
		this.lastUpdatedDate = lastUpdatedDate;
	}

	@Column(name = "CUR_APPLY_QTY")
	public int getCurApplyQty() {
		return curApplyQty;
	}

	public void setCurApplyQty(int curApplyQty) {
		this.curApplyQty = curApplyQty;
	}

	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name = "IF_ID")
	public Http getHttp() {
		return http;
	}

	public void setHttp(Http http) {
		this.http = http;
	}

	@Column(name = "IF_TOKEN_STATUS", length = 2)
	public String getStatus() {
		return status;
	}

	public void setStatus(String status) {
		this.status = status;
	}

	@Transient
	public String getFieldName() {
		return fieldName;
	}

	public void setFieldName(String fieldName) {
		this.fieldName = fieldName;
	}

	@Transient
	public String getInterfaceCallSytemName() {
		return interfaceCallSytemName;
	}

	public void setInterfaceCallSytemName(String interfaceCallSytemName) {
		this.interfaceCallSytemName = interfaceCallSytemName;
	}

	@Transient
	public String getInterfacePubSystemName() {
		return interfacePubSystemName;
	}

	public void setInterfacePubSystemName(String interfacePubSystemName) {
		this.interfacePubSystemName = interfacePubSystemName;
	}

	@Transient
	public String getTokenId() {
		return id;
	}

	@Transient
	public String getApplyDateStr() {
		return FASTDATEFORMAT_YYYYMMDDHHMMSS.format(this.applyDate);
	}
	@Transient
	public String getKeyInvalidDateStr() {
		return FASTDATEFORMAT_YYYYMMDDHHMMSS.format(this.keyInvalidDate);
	}
}
